package com.recharge.service.impl;

import com.alibaba.druid.sql.ast.expr.SQLCaseExpr.Item;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.recharge.common.domain.PageDO;
import com.recharge.common.enums.ErrorCode;
import com.recharge.common.result.ResultDO;
import com.recharge.common.result.ResultSupport;
import com.recharge.common.util.BeanUtilsExtends;
import com.recharge.common.util.Constant;
import com.recharge.core.entity.*;
import com.recharge.core.mapper.RoleMapper;
import com.recharge.core.mapper.RolePermissionMapper;
import com.recharge.core.mapper.UserLoginMapper;
import com.recharge.core.mapper.UserRoleMapper;
import com.recharge.domain.context.ShiroUtils;
import com.recharge.domain.query.RoleQueryDO;
import com.recharge.domain.vo.MenuTreeDO;
import com.recharge.domain.vo.ParamDO;
import com.recharge.domain.vo.PermissionDO;
import com.recharge.domain.vo.RoleDO;
import com.recharge.service.PermissionService;
import com.recharge.service.RoleService;
import com.recharge.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色 服务实现类
 * </p>
 *
 * @author reagan
 * @since 2020-08-19
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RoleEntity> implements RoleService {

    private Logger logger = LoggerFactory.getLogger(this.getClass().getName());

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private UserLoginMapper userLoginMapper;

    @Autowired
    private PermissionService permissionService;
    
    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Transactional(propagation= Propagation.REQUIRED, rollbackFor= Exception.class)
    public ResultDO bindRole(String userId, String[] roleIds) {

        ResultDO result = new ResultSupport();
        if(roleIds == null || roleIds.length <=0){
            return result;
        }
        for(String roleId : roleIds) {
            UserRoleEntity entity = new UserRoleEntity();
            entity.setUserId(userId);
            entity.setRoleId(roleId);
            int res = -1;
            try {
                res = userRoleMapper.insert(entity);
            } catch (Exception e) {
                result.setSuccess(false);
                result.setErrorCode(ErrorCode.SYSTEM_EXCEPTION);
                logger.debug("error code：" + ErrorCode.SYSTEM_EXCEPTION.getCode() + " msg " + ErrorCode.SYSTEM_EXCEPTION.getMsg());
                return result;
            }
            //判断r的值是否小于0.如果小于0 就代表添加失败
            if (res < -1) {
                result.setSuccess(false);
                result.setErrorCode(ErrorCode.ROLE_BIND_USER_ERROR);
                logger.debug("error code：" + ErrorCode.ROLE_BIND_USER_ERROR.getCode() + " msg " + ErrorCode.ROLE_BIND_USER_ERROR.getMsg());
                return result;
            }
        }

        return result;
    }

    @Transactional(propagation= Propagation.REQUIRED, rollbackFor= Exception.class)
    public ResultDO unbindRoleAll(String userId) {
        ResultDO result = new ResultSupport();

        QueryWrapper<UserRoleEntity> queryWrapper = new QueryWrapper<UserRoleEntity>();
        queryWrapper.lambda().eq(UserRoleEntity::getUserId, userId);

        int res = -1;
        try {
            res = userRoleMapper.delete(queryWrapper);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.SYSTEM_EXCEPTION);
            logger.debug("error code：" + ErrorCode.SYSTEM_EXCEPTION.getCode() + " msg" + ErrorCode.SYSTEM_EXCEPTION.getMsg());
            return result;
        }

        if(res < 0) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.ROLE_UNBIND_USER_ALL_ERROR);
            logger.debug("error code：" + ErrorCode.ROLE_UNBIND_USER_ALL_ERROR.getCode() + " msg" + ErrorCode.ROLE_UNBIND_USER_ALL_ERROR.getMsg());
            return result;
        }
        return result;
    }

    @Transactional(propagation= Propagation.REQUIRED, rollbackFor= Exception.class)
    public ResultDO add(RoleDO domain) {

        RoleEntity entity = new RoleEntity();

        ResultDO result = BeanUtilsExtends.copy(entity, domain);
        if (!result.isSuccess()) {
            logger.debug("error code：" + result.getErrorCode().getCode() + " msg" + result.getErrorCode().getMsg());
            return result;
        }

        int count = 0;
        try {
            count = baseMapper.selectCount(Wrappers.<RoleEntity>lambdaQuery().eq(RoleEntity::getCode, domain.getCode()));
        } catch(Exception e) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.SYSTEM_EXCEPTION);
            logger.debug("error code：" + ErrorCode.SYSTEM_EXCEPTION.getCode() + " msg" + ErrorCode.SYSTEM_EXCEPTION.getMsg());
            return result;
        }

        if(count > 0){
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.ROLE_CODE_EXIT);
            logger.debug("error code：" + ErrorCode.ROLE_CODE_EXIT.getCode() + " msg" + ErrorCode.ROLE_CODE_EXIT.getMsg());
            return result;
        }

        entity.setCreateDate(Calendar.getInstance().getTime());
        entity.setModifiDate(entity.getCreateDate());
        entity.setModifier(entity.getCreator());

        boolean res = false;
        try {
            res = this.save(entity);
        } catch(Exception e) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.SYSTEM_EXCEPTION);
            logger.debug("error code：" + ErrorCode.SYSTEM_EXCEPTION.getCode() + " msg" + ErrorCode.SYSTEM_EXCEPTION.getMsg());
            return result;
        }

        //判断r的值是否小于0.如果小于0 就代表添加失败
        if(!res) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.USER_ADD_ERROR);
            logger.debug("error code：" + ErrorCode.USER_ADD_ERROR.getCode() + " msg" + ErrorCode.USER_ADD_ERROR.getMsg());
            return result;
        }

        result = permissionService.bindRolePermission(entity.getId(), domain.getPermissionIds());
        if(!result.isSuccess()) {
            try {
                throw new Exception(result.getErrorCode().getMsg());
            } catch (Exception e) {
                logger.debug("error code：" + result.getErrorCode().getCode() + " msg" + result.getErrorCode().getMsg());
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return result;
            }

        }
        return result;
    }

    @Transactional(propagation= Propagation.REQUIRED, rollbackFor= Exception.class)
    public ResultDO edit(RoleDO domain) {

        ResultDO result = permissionService.unRolePermissionAll(domain.getId());

        if(!result.isSuccess()) {
            logger.debug("error code：" + result.getErrorCode().getCode() + " msg" + result.getErrorCode().getMsg());
            return result;
        }

        RoleEntity entity = new RoleEntity();

        result = BeanUtilsExtends.copy(entity, domain);
        if (!result.isSuccess()) {
            logger.debug("error code：" + result.getErrorCode().getCode() + " msg" + result.getErrorCode().getMsg());
            return result;
        }

        entity.setModifiDate(Calendar.getInstance().getTime());

        boolean res = false;
        try {
            res = this.updateById(entity);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.SYSTEM_EXCEPTION);
            logger.debug("error code：" + ErrorCode.SYSTEM_EXCEPTION.getCode() + " msg" + ErrorCode.SYSTEM_EXCEPTION.getMsg());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return result;
        }

        //判断r的值是否小于0.如果小于0 就代表添加失败
        if(!res) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.USER_EDIT_ERROR);
            logger.debug("error code：" + ErrorCode.USER_EDIT_ERROR.getCode() + " msg" + ErrorCode.USER_EDIT_PASSWORD_ERROR.getMsg());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return result;
        }

        result = permissionService.bindRolePermission(entity.getId(), domain.getPermissionIds());
        if(!result.isSuccess()) {
            try {
                throw new Exception(result.getErrorCode().getMsg());
            } catch (Exception e) {
                logger.debug("error code：" + result.getErrorCode().getCode() + " msg" + result.getErrorCode().getMsg());
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return result;
            }
        }
        //剔除该角色的在线用户
        List<UserRoleEntity> userRoleList = userRoleMapper.selectList(Wrappers.<UserRoleEntity>lambdaQuery().eq(UserRoleEntity::getRoleId, domain.getId()));
        if(userRoleList != null && userRoleList.size() >0){
            List<UserLoginEntity> userList = userLoginMapper.selectBatchIds(userRoleList.stream().map(UserRoleEntity::getUserId).collect(Collectors.toList()));
            if(userList != null && userList.size() >0){
                userList.stream().forEach( item ->{
                    ShiroUtils.deleteCache(item.getUsername(), true);
                });
            }
        }

        return result;
    }

    
    public ResultDO get(String id) {

        ResultDO result = new ResultSupport();
        RoleEntity entity = null;

        try {
            entity = this.getById(id);
        } catch(Exception e) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.SYSTEM_EXCEPTION);
            logger.debug("error code：" + ErrorCode.SYSTEM_EXCEPTION.getCode() + " msg" + ErrorCode.SYSTEM_EXCEPTION.getMsg());
            return result;
        }
        RoleDO domain = getDomain(entity);
        List<RolePermissionEntity> rolePerList = null;
        try {
            rolePerList = rolePermissionMapper.selectList(Wrappers.<RolePermissionEntity>lambdaQuery().eq(RolePermissionEntity::getRoleId, id));
        } catch(Exception e) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.SYSTEM_EXCEPTION);
            logger.debug("error code：" + ErrorCode.SYSTEM_EXCEPTION.getCode() + " msg" + ErrorCode.SYSTEM_EXCEPTION.getMsg());
            return result;
        }
        String[] rolePerArray = new String[0];
        if(rolePerList != null && rolePerList.size() >0){

            result = permissionService.getPermissionByRoleId(entity.getId());
            if(!result.isSuccess()) {
                logger.debug("error code：" + result.getErrorCode().getCode() + " msg" + result.getErrorCode().getMsg());
                return result;
            }
            List<PermissionDO> permissionList =  (List<PermissionDO>) result.getModel(ResultDO.FIRST_MODEL_KEY);

            List<MenuTreeDO> menuTreeList = getRolePermissionTreeDO(permissionList, Constant.TREE_ROOT_ID, "0");

            List<String> permissionIdList = new ArrayList<>();

            getChildId(menuTreeList,permissionIdList);

            String[] array = permissionIdList.stream().distinct().toArray(String[]::new);
            domain.setPermissionIds(array);
        } else {
            domain.setPermissionIds(rolePerArray);
        }
        result.setModel(ResultDO.FIRST_MODEL_KEY, domain);

        return result;
    }

    @Transactional(propagation= Propagation.REQUIRED, rollbackFor= Exception.class)
    public ResultDO remove(String id, String modifier) {

        ResultDO result = permissionService.unRolePermissionAll(id);

        if(!result.isSuccess()) {
            logger.debug("error code：" + result.getErrorCode().getCode() + " msg" + result.getErrorCode().getMsg());
            return result;
        }

        RoleEntity entity = new RoleEntity();

        entity.setModifiDate(Calendar.getInstance().getTime());
        entity.setModifier(modifier);
        entity.setId(id);
        entity.setIsDelete(Constant.IS_DELETE);

        boolean res = false;
        try {
            res = this.updateById(entity);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.SYSTEM_EXCEPTION);
            logger.debug("error code：" + ErrorCode.SYSTEM_EXCEPTION.getCode() + " msg" + ErrorCode.SYSTEM_EXCEPTION.getMsg());
            return result;
        }

        //判断r的值是否小于0.如果小于0 就代表添加失败
        if(!res) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.USER_DELETE_ERROR);
            logger.debug("error code：" + ErrorCode.USER_DELETE_ERROR.getCode() + " msg" + ErrorCode.USER_DELETE_ERROR.getMsg());
            return result;
        }

        return result;
    }

    
    public ResultDO find(RoleQueryDO query) {

        ResultDO result = new ResultSupport();

        QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<RoleEntity>();

        queryWrapper.lambda().eq(RoleEntity::getIsDelete, Constant.IS_DELETE_FALSE);

        Page<RoleEntity> page = new Page<>(query.getCurrentPage(),query.getPageSize());

        IPage<RoleEntity> iPage = null;

        try {
            iPage = baseMapper.selectPage(page, queryWrapper);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.SYSTEM_EXCEPTION);
            logger.debug("error code：" + ErrorCode.SYSTEM_EXCEPTION.getCode() + " msg" + ErrorCode.SYSTEM_EXCEPTION.getMsg());
            return result;
        }
        List<RoleDO> doList = getDomainList(iPage.getRecords());
        PageDO<RoleDO> pageDO = new PageDO<>();
        pageDO.setCurrentPage(iPage.getCurrent());
        pageDO.setPageCount(iPage.getPages());
        pageDO.setPageSize(iPage.getSize());
        pageDO.setRowCount(iPage.getTotal());
        pageDO.setData(doList);
        result.setModel(ResultDO.FIRST_MODEL_KEY, pageDO);
        return result;
    }

    public ResultDO getRoleByUserId(String userId) {
        ResultDO baseResult = new ResultSupport();
        QueryWrapper<UserRoleEntity> queryWrapper = new QueryWrapper<UserRoleEntity>();
        queryWrapper.lambda().eq(UserRoleEntity::getUserId, userId);

        List<UserRoleEntity> list = null;
        try {
            list = this.userRoleMapper.selectList(queryWrapper);
        } catch (Exception e) {
        	baseResult.setSuccess(false);
        	baseResult.setErrorCode(ErrorCode.SYSTEM_EXCEPTION);
            logger.debug("error code：" + ErrorCode.SYSTEM_EXCEPTION.getCode() + " msg" + ErrorCode.SYSTEM_EXCEPTION.getMsg());
            return baseResult;
        }
        if(list == null || list.isEmpty()) {
       	 return baseResult;
       }
        
        List<String> roleIds = list.stream().map(UserRoleEntity::getRoleId).distinct().collect(Collectors.toList());
        
        //根据用户的所有角色ID查询所有的角色信息
        ResultDO result = getBatchIds(roleIds);
        
        if(!result.isSuccess()) {
        	result.setModel(ResultDO.FIRST_MODEL_KEY, null);
        }
        
        baseResult.setModel(ResultDO.FIRST_MODEL_KEY, result.getModel(ResultDO.FIRST_MODEL_KEY));
        
        //根据用户的所有角色查询所有的菜单
        
        result = permissionService.getTreeByRoleIds(roleIds);
        
        if(!result.isSuccess()) {
        	baseResult.setModel(ResultDO.SECOND_MODEL_KEY, null);
        }
        
        List<PermissionDO> permissionList = (List<PermissionDO>) result.getModel(ResultDO.FIRST_MODEL_KEY);
        
        List<MenuTreeDO> menuTreeList = getMenuTreeDO(permissionList, Constant.TREE_ROOT_ID, "0");
        
        baseResult.setModel(ResultDO.THIRD_MODEL_KEY, menuTreeList);

        baseResult.setModel(ResultDO.SECOND_MODEL_KEY, permissionList);

        return baseResult;
    }

    public ResultDO getRoleIdsByUserId(String userId) {
        ResultDO baseResult = new ResultSupport();
        QueryWrapper<UserRoleEntity> queryWrapper = new QueryWrapper<UserRoleEntity>();
        queryWrapper.lambda().eq(UserRoleEntity::getUserId, userId);

        List<UserRoleEntity> list = null;
        try {
            list = this.userRoleMapper.selectList(queryWrapper);
        } catch (Exception e) {
            baseResult.setSuccess(false);
            baseResult.setErrorCode(ErrorCode.SYSTEM_EXCEPTION);
            logger.debug("error code：" + ErrorCode.SYSTEM_EXCEPTION.getCode() + " msg" + ErrorCode.SYSTEM_EXCEPTION.getMsg());
            return baseResult;
        }
        if(list == null || list.isEmpty()) {
            return baseResult;
        }

        List<String> roleIds = list.stream().map(UserRoleEntity::getRoleId).distinct().collect(Collectors.toList());

        //根据用户的所有角色ID查询所有的角色信息

        baseResult.setModel(ResultDO.FIRST_MODEL_KEY, roleIds);

        return baseResult;
    }
    
	@Override
	public ResultDO getBatchIds(List<String> ids) {
		ResultDO result = new ResultSupport();
		if(ids == null || ids.isEmpty()) {
       	 	return result;
		}
		List<RoleEntity> roleList = null;
		try {
			roleList = baseMapper.selectBatchIds(ids);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.SYSTEM_EXCEPTION);
            logger.debug("error code：" + ErrorCode.SYSTEM_EXCEPTION.getCode() + " msg" + ErrorCode.SYSTEM_EXCEPTION.getMsg());
            return result;
        }
		
		if(roleList == null || roleList.isEmpty()) {
       	 	return result;
		}
		
		List<RoleDO> doList = getDomainList(roleList);
		
		result.setModel(ResultDO.FIRST_MODEL_KEY, doList);
		return result;
	}

    public ResultDO getRolePermissionByUserId(String userId) {
        ResultDO result = new ResultSupport();
        QueryWrapper<UserRoleEntity> queryWrapper = new QueryWrapper<UserRoleEntity>();
        queryWrapper.eq("user_id", userId);

        List<UserRoleEntity> list = null;
        try {
            list = this.userRoleMapper.selectList(queryWrapper);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.SYSTEM_EXCEPTION);
            logger.debug("error code：" + ErrorCode.SYSTEM_EXCEPTION.getCode() + " msg" + ErrorCode.SYSTEM_EXCEPTION.getMsg());
            return result;
        }


        List<RoleDO> roleList = new ArrayList<>();

        for(UserRoleEntity entity : list) {
            result = permissionService.getPermissionByRoleId(entity.getRoleId());
            if(!result.isSuccess()) {
                logger.debug("error code：" + result.getErrorCode().getCode() + " msg" + result.getErrorCode().getMsg());
                return result;
            }
            List<PermissionDO> permissionList = (List<PermissionDO>) result.getModel(ResultDO.FIRST_MODEL_KEY);

            result = get(entity.getRoleId());
            if(!result.isSuccess()) {
                logger.debug("error code：" + result.getErrorCode().getCode() + " msg" + result.getErrorCode().getMsg());
                return result;
            }
            RoleDO domain = (RoleDO) result.getModel(ResultDO.FIRST_MODEL_KEY);
            domain.setPermissiones(permissionList);
            roleList.add(domain);
        }

        result.setModel(ResultDO.FIRST_MODEL_KEY, roleList);

        return result;
    }

    @Override
    public ResultDO selectRoleByPermissionId(String permissionId) {
        ResultDO result = new ResultSupport();
        QueryWrapper<RolePermissionEntity> queryWrapper = new QueryWrapper<RolePermissionEntity>();
        queryWrapper.lambda().eq(RolePermissionEntity::getPermissionId, permissionId);

        List<RolePermissionEntity> list = null;
        try {
            list = rolePermissionMapper.selectList(queryWrapper);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.SYSTEM_EXCEPTION);
            logger.debug("error code：" + ErrorCode.SYSTEM_EXCEPTION.getCode() + " msg" + ErrorCode.SYSTEM_EXCEPTION.getMsg());
            return result;
        }
        List<RoleDO> roleList = new ArrayList<>();
        for(RolePermissionEntity entity : list) {
            result = permissionService.getPermissionByRoleId(entity.getRoleId());
            if(!result.isSuccess()) {
                logger.debug("error code：" + result.getErrorCode().getCode() + " msg" + result.getErrorCode().getMsg());
                return result;
            }
            List<PermissionDO> permissionList = (List<PermissionDO>) result.getModel(ResultDO.FIRST_MODEL_KEY);

            result = get(entity.getRoleId());
            if(!result.isSuccess()) {
                logger.debug("error code：" + result.getErrorCode().getCode() + " msg" + result.getErrorCode().getMsg());
                return result;
            }
            RoleDO domain = (RoleDO) result.getModel(ResultDO.FIRST_MODEL_KEY);
            domain.setPermissiones(permissionList);
            roleList.add(domain);
        }

        result.setModel(ResultDO.FIRST_MODEL_KEY, roleList.stream().distinct().collect(Collectors.toList()));

        return result;
    }


    private RoleDO getDomain(RoleEntity entity) {

        //判断房源对象是否为空，为空就返回 空，不为空就进行下面的代码
        if(entity == null) return null;
        RoleDO domain = new RoleDO();

        domain = BeanUtilsExtends.copyProperties(domain, entity) ? domain : null;

        //判断拷贝数据到Dto对象是否成功，如果成功则返回domian，如果不成功就返回空
        return domain;
    }

    private List<RoleDO> getDomainList(List<RoleEntity> list) {
        //创建一个名为dolist的房源do类型的 list集合
        List<RoleDO> doList = new ArrayList<RoleDO>();

        //判断传入的list的集合的size是否大于0
        if(list.size() > 0) {
            //大于0是，遍历list，创建do类型的对象 调用getdomain方法
            for(RoleEntity entity : list) {

                RoleDO domain = this.getDomain(entity);

                //判断得到的domain 是否为空 不为空 就往dolist集合里面添加domian 为空就返回空
                if(domain == null) {
                    return null;
                }
                doList.add(domain);
            }
        } else {
            return null;
        }
        return doList;
    }
    
    private List<MenuTreeDO> getMenuTreeDO(List<PermissionDO> list, String parentId, String level){
    	if(list == null || list.isEmpty()) {
       	 	return null;
		}
    	List<MenuTreeDO> treeList = new ArrayList<MenuTreeDO>();
    	
    	list.stream().filter(Objects::nonNull).forEach( item ->{
    	    if(item.getType() == Integer.parseInt(Constant.MENU)){
                MenuTreeDO treeDO = new MenuTreeDO();
                treeDO.setId(item.getId());
                treeDO.setName(item.getName());
                treeDO.setType(item.getType());
                treeDO.setIcon(item.getIcon());
                treeDO.setUri(item.getComponent());
                if(parentId.equals(item.getPermissionId())) {
                    treeDO.setLevel(level);
                    treeDO.setChildren(getMenuTreeDO(list, item.getId(), String.valueOf(Integer.parseInt(level) + 1)));
                    treeList.add(treeDO);
                }
            }
    	});
    	return treeList;
    }

    private List<MenuTreeDO> getRolePermissionTreeDO(List<PermissionDO> list, String parentId, String level){
        if(list == null || list.isEmpty()) {
            return null;
        }
        List<MenuTreeDO> treeList = new ArrayList<MenuTreeDO>();

        list.stream().filter(Objects::nonNull).forEach( item ->{
            MenuTreeDO treeDO = new MenuTreeDO();
            treeDO.setId(item.getId());
            treeDO.setName(item.getName());
            treeDO.setType(item.getType());
            treeDO.setIcon(item.getIcon());
            treeDO.setUri(item.getComponent());
            if(parentId.equals(item.getPermissionId())) {
                treeDO.setLevel(level);
                treeDO.setChildren(getRolePermissionTreeDO(list, item.getId(), String.valueOf(Integer.parseInt(level) + 1)));
                treeList.add(treeDO);
            }
        });
        return treeList;
    }


    private void getChildId(List<MenuTreeDO> list, List<String> childIdArray){
        list.stream().forEach( item ->{
            if(item.getChildren() != null && item.getChildren().size() > 0){
                getChildId(item.getChildren(), childIdArray);
            } else {
                childIdArray.add(item.getId());
            }
        });
    }
}
